package org.deltaset.secured.user;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;

/**
 *
 * @author dmakariev
 */
public class UserContextBuilder {

    UserContextBuilder() {
        //TODO: check
        final Locale currentLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
        final ResourceBundle deltasetConfig = ResourceBundle.getBundle("DeltasetConfig", currentLocale);

        //deltasetConfig properties
        dateFormat = deltasetConfig.getString("dateFormat");
        showPager = deltasetConfig.getString("showPager");
        fieldsetPosition = deltasetConfig.getString("fieldsetPosition");
        fieldsetWidth = deltasetConfig.getString("fieldsetWidth");
        viewType = deltasetConfig.getString("viewType");
        //application specific
        locale = currentLocale.toString();
        layoutName = "left";
        themeName = "start";
        uiStyle = "flow";
    }

    UserContextBuilder(UserContext uc) {
        //deltasetConfig properties
        dateFormat = uc.getDateFormat();
        showPager = uc.getShowPager();
        fieldsetPosition = uc.getFieldsetPosition();
        fieldsetWidth = uc.getFieldsetWidth();
        viewType = uc.getViewType();
        //application specific
        locale = uc.getLocale();
        themeName = uc.getThemeName();
        layoutName = uc.getLayoutName();
    }
    //deltasetConfig properties
    private String dateFormat;
    private String showPager;
    private String fieldsetPosition;
    private String fieldsetWidth;
    private String viewType;
    private String uiStyle;
    //application specific
    private String locale;
    private String themeName;
    private String layoutName;

    static UserContextBuilder from(UserContext uc) {
        return new UserContextBuilder(uc);
    }

    static UserContext newUserContext() {
        return new UserContextBuilder().build();
    }

    public UserContext build() {
        final UserContext result = new UserContext(
                dateFormat,
                showPager,
                fieldsetPosition,
                fieldsetWidth,
                viewType,
                locale,
                themeName,
                layoutName,
                uiStyle);
        return result;
    }

    //deltasetConfig properties
    public UserContextBuilder dateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
        return this;
    }

    public UserContextBuilder showPager(String showPager) {
        this.showPager = showPager;
        return this;
    }

    public UserContextBuilder fieldsetPosition(String fieldsetPosition) {
        this.fieldsetPosition = fieldsetPosition;
        return this;
    }

    public UserContextBuilder fieldsetWidth(String fieldsetWidth) {
        this.fieldsetWidth = fieldsetWidth;
        return this;
    }

    public UserContextBuilder viewType(String viewType) {
        this.viewType = viewType;
        return this;
    }

    public UserContextBuilder locale(String locale) {
        this.locale = locale;
        return this;
    }

    public UserContextBuilder themeName(String themeName) {
        this.themeName = themeName;
        return this;
    }

    public UserContextBuilder layoutName(String layoutName) {
        this.layoutName = layoutName;
        return this;
    }
    
    public UserContextBuilder uiStyle(String uiStyle) {
        this.uiStyle = uiStyle;
        return this;
    }
}
